package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.TripplanSubwayItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeWeathRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearMetroStandbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfo;

/**
 * Copyright (c)2020
 * author: Clark
 * created on:2020年10月7日 16点06分
 * description: App出行规划地铁模块
 */
public class TripPlanSubwayListAdapter extends BaseRecyclerAdapter<TripPlanSubwayListAdapter.TripPlanSubwayListHolder> {
    private Context mContext;
    private QueryNearMetroStandbyRes viewData;
    private BaseInterface baseInterface;


    public interface BaseInterface {
        public void onClick(View view, QueryNearMetroStandbyRes.MetroSiteListBean bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public TripPlanSubwayListAdapter(Context context ) {
        mContext = context;

    }


    @Override
    public TripPlanSubwayListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_head_view_new, parent, false);
        return  TripPlanSubwayListHolder.creat(parent);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.tripplan_subway_item;
    }

    @Override
    public void onBindViewHolder(TripPlanSubwayListHolder holder, int position) {
//        holder.blurringView.setBlurredView();

        loadItemData(position, holder.binding);

    }
    private void loadItemData(int position, TripplanSubwayItemBinding binding) {
        if (viewData != null) {

            QueryNearMetroStandbyRes.MetroSiteListBean bean=viewData.getMetroSiteList().get(position);
            binding.subwayStationName.setText(bean.getStandName());
            binding.subwayStationAddress.setText(bean.getStandAddress());
            if(Float.valueOf(bean.getDistance())>=1000){
                float distance=(Float.valueOf(bean.getDistance())/1000);
                String distanceString=String.valueOf(distance);
                binding.subwayDistance.setText(String.format(mContext.getResources().getString(R.string.address_distance_km),distanceString.substring(0,distanceString.indexOf(".")+2)));
            }else{
                binding.subwayDistance.setText(String.format(mContext.getResources().getString(R.string.address_distance_m),bean.getDistance()));
            }
            binding.subwayStationAddress.setText(bean.getStandAddress());

            ArrayList<String> metroList=new ArrayList<>();

            if(viewData.getMetroSiteList().get(position).getCrossLineName()==null){
                metroList.add(viewData.getMetroSiteList().get(position).getLineName());
            }else {
                String []crossLineName=viewData.getMetroSiteList().get(position).getCrossLineName().split(",");
                for(String s:crossLineName){
                    metroList.add(s);
                }
            }




            binding.subwayStationGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    baseInterface.onClick(v, bean);
                }
            });

            binding.itemLayout.setOnClickListener(v->{
                baseInterface.onClick(v, bean);
            });
        }

    }
    @Override
    public int getItemCount() {
//        if(viewData.getMetroSiteList().size()>2){
//            return 2;
//        }else {
            return viewData.getMetroSiteList().size();
//        }

    }


    public void setViewData(QueryNearMetroStandbyRes viewData) {
        this.viewData = viewData;
    }


    public static class TripPlanSubwayListHolder extends BaseHolder {
        private TripplanSubwayItemBinding binding;

        public static TripPlanSubwayListHolder creat(ViewGroup parent) {
            TripplanSubwayItemBinding binding =
                    TripplanSubwayItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new TripPlanSubwayListHolder(binding);
        }

        public TripPlanSubwayListHolder(TripplanSubwayItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }

}
